/*
 * Copyright (C) 2011, Pavel Samko <bulldozerbsg@gmail.com>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the "Pavel Samko" nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 */

#include "VCThread.h"

#if VC_PLATFORM_WIN && !VC_USE_PTHREAD
#if _WIN_CRT
unsigned int __stdcall VCThreadFunc(void * param)
#else
DWORD WINAPI VCThreadFunc(LPVOID param)
#endif
#else
void * VCThreadFunc(void * param)
#endif
{
	VCThread * thread_obj = static_cast<VCThread *>(param);
	if (thread_obj)
	{
		int exitcode = -1;
		try
		{
			exitcode = thread_obj->Execute(thread_obj->m_data);
		} catch (...)
		{
			TRACE("THREAD EXCEPTION!!!")
		}
		thread_obj->Exit(exitcode);
	}
#if VC_PLATFORM_WIN && !VC_USE_PTHREAD
	return 0;
#else
	return NULL;
#endif
}

VCThread::VCThread(void * data)
: m_exitcode(0)
, m_data(data)
{
#if VC_PLATFORM_WIN && !VC_USE_PTHREAD
#if _WIN_CRT
	m_thread = (HANDLE)_beginthreadex(NULL, 0, VCThreadFunc, static_cast<void *>(this), 0, NULL);
#else
	m_thread = CreateThread(NULL, 0, VCThreadFunc, static_cast<LPVOID>(this), 0, NULL);
#endif
#else
	pthread_create(&m_thread, NULL, VCThreadFunc, static_cast<void *>(this));
#endif
}

VCThread::~VCThread()
{
	Wait();
}

int VCThread::Wait()
{
#if VC_PLATFORM_WIN && !VC_USE_PTHREAD
	if (m_thread != INVALID_HANDLE_VALUE)
	{
		WaitForSingleObject(m_thread, INFINITE);
		CloseHandle(m_thread);
		m_thread = INVALID_HANDLE_VALUE;
	}
#else
	pthread_join(m_thread, NULL);
#endif
	return m_exitcode;
}

int VCThread::Execute(void * data)
{
	// need override
	return 0;
}

void VCThread::OnExit(int exitcode)
{
	// need override
}

void VCThread::Exit(int exitcode)
{
	m_exitcode = exitcode;
	OnExit(m_exitcode);
#if VC_PLATFORM_WIN && !VC_USE_PTHREAD
#if _WIN_CRT
	_endthreadex(0);
#else
	ExitThread(0);
#endif
#else
	pthread_exit(NULL);
#endif
}

